package com.letian.web.controller.info;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.letian.common.constant.Constants;
import com.letian.common.core.domain.entity.SysUser;
import com.letian.common.core.domain.model.UsersVO;
import com.letian.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.info.domain.InfoComment;
import com.letian.info.service.IInfoCommentService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 评论信息Controller
 * 
 * @author bmz
 * @date 2023-04-05
 */
@RestController
@RequestMapping("/info/comment")
public class InfoCommentController extends BaseController
{
    @Autowired
    private IInfoCommentService infoCommentService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询评论信息列表
     */
    @PreAuthorize("@ss.hasPermi('info:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoComment infoComment)
    {
        startPage();
        List<InfoComment> list = infoCommentService.selectInfoCommentList(infoComment);
        return getDataTable(list);
    }

    /**
     * 查询评论信息列表
     */
    @GetMapping("/list/article/{articleId}")
    public TableDataInfo articleCommentList(@PathVariable("articleId") Long articleId)
    {
        startPage();
        InfoComment infoComment = new InfoComment();
        infoComment.setObjectType(Constants.OBJECT_TYPE_ARTICLE_STR);
        infoComment.setObjectId(articleId);
        infoComment.setDelFlag(Constants.SYS_DEL_NO);
        List<InfoComment> list = infoCommentService.selectInfoCommentList(infoComment);
        for (InfoComment item: list) {
            SysUser user= sysUserService.selectUserByUserName(item.getCommentBy());
            UsersVO usersVO = new UsersVO();
            BeanUtils.copyProperties(user,usersVO);
            item.setCommentUser(usersVO);
        }
        return getDataTable(list);
    }

    /**
     * 导出评论信息列表
     */
    @PreAuthorize("@ss.hasPermi('info:comment:export')")
    @Log(title = "评论信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoComment infoComment)
    {
        List<InfoComment> list = infoCommentService.selectInfoCommentList(infoComment);
        ExcelUtil<InfoComment> util = new ExcelUtil<InfoComment>(InfoComment.class);
        util.exportExcel(response, list, "评论信息数据");
    }

    /**
     * 获取评论信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('info:comment:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId)
    {
        return success(infoCommentService.selectInfoCommentByCommentId(commentId));
    }

    /**
     * 新增评论信息
     */
    @PreAuthorize("@ss.hasPermi('info:comment:add')")
    @Log(title = "评论信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoComment infoComment)
    {
        infoComment.setCommentBy(getUsername());
        infoComment.setCreateBy(getUsername());
        return toAjax(infoCommentService.insertInfoComment(infoComment));
    }

    /**
     * 修改评论信息
     */
    @PreAuthorize("@ss.hasPermi('info:comment:edit')")
    @Log(title = "评论信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoComment infoComment)
    {
        infoComment.setUpdateBy(getUsername());
        return toAjax(infoCommentService.updateInfoComment(infoComment));
    }

    /**
     * 删除评论信息
     */
    @PreAuthorize("@ss.hasPermi('info:comment:remove')")
    @Log(title = "评论信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(infoCommentService.deleteInfoCommentByCommentIds(commentIds));
    }
}
